package org.weasel.wizards;

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.wizard.Wizard;
import org.weasel.business.Developer;
import org.weasel.business.IDFactory;
import org.weasel.business.UserStory;
import org.weasel.controllers.TreeController;
import org.weasel.tree.TreeObject;
import org.weasel.tree.TreeObjectIteration;
import org.weasel.tree.TreeObjectProject;
import org.weasel.tree.TreeObjectRelease;
import org.weasel.tree.TreeObjectUnscheduled;
import org.weasel.tree.TreeParent;

public class UserStoryWizard extends Wizard {
	private UserStoryWizardPage1 wizardPage1;
	private UserStoryWizardPage2 wizardPage2;
	private ISelection selection;
	private TreeObjectProject to_project;
	private TreeObjectRelease to_release = null;
	private TreeObjectIteration to_iteration = null;
	private TreeObjectUnscheduled to_unscheduled = null;
	
	public UserStoryWizard(TreeObjectProject to_project, TreeParent treeParent) {
		super();
		initialize();
		this.to_project = to_project;
		if(treeParent instanceof TreeObjectRelease) {
			this.to_release = (TreeObjectRelease) treeParent;
		} else if(treeParent instanceof TreeObjectIteration) {
			this.to_iteration = (TreeObjectIteration) treeParent;
		} else if(treeParent instanceof TreeObjectUnscheduled) {
			this.to_unscheduled = (TreeObjectUnscheduled) treeParent;
		}
	}	
	
	private void initialize() {
		setWindowTitle("Create new Userstory for this project ");
		setNeedsProgressMonitor(true);
	}
	
	@Override
	public void addPages() {
        wizardPage1 = new UserStoryWizardPage1(selection);
        addPage(wizardPage1);
        boolean isIteration;
        if(to_iteration != null)
        	isIteration = true;
        else
        	isIteration = false;
        wizardPage2 = new UserStoryWizardPage2(selection, this.to_project.getProject(), isIteration);
        addPage(wizardPage2);
    }

	@Override
	public boolean performFinish() {
		String usName = wizardPage1.getUserstoryName();
		String generatedDate =  new SimpleDateFormat("dd.MM.yyyy").format(new Date());
		String usDescription = wizardPage1.getUserstoryDescription();
		String estimatedBusinessValue = wizardPage2.getEstimatedBusinessValue();
		String usEstimatedTime = wizardPage2.getEstimatedTime();
		boolean experimentCard = wizardPage1.isExperimentCard();
		
		String estimatedRisk2 = wizardPage2.getEstimatedRisk();
		int estimatedRisk = Integer.parseInt(estimatedRisk2);
		Time estimatedTime = Time.valueOf(usEstimatedTime);
		Time usedTime = Time.valueOf("00:00:00");
		
		int businessValue = Integer.parseInt(estimatedBusinessValue);
		
		ArrayList<Developer> developers = wizardPage2.getDevelopers(); 
		
		UserStory userStory = new UserStory(new IDFactory().getUniqueId(), usName,generatedDate, null, usDescription, estimatedTime, usedTime, estimatedRisk, businessValue, experimentCard, developers, null);
		TreeController treeController = TreeController.instance();
		try {
			if(isProject()) {
				TreeObject[] children = to_project.getChildren();
				treeController.addUserStory(userStory, (TreeParent)children[1]);
			} else if(isRelease()) {
				TreeObject[] children = to_release.getChildren();
				//for some reason the position of the Unscheduled Element varies if more than 1 Iteration is added..therefore..
				for (TreeObject object : children) {
					if(object instanceof TreeObjectUnscheduled){
						treeController.addUserStory(userStory, (TreeParent)object);
					}
				}
			} else if(isIteration()) {
				treeController.addUserStory(userStory, to_iteration);
			} else if(isUnscheduled()) {
				treeController.addUserStory(userStory, to_unscheduled);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}
	
	private boolean isProject() {
		if(to_iteration == null && to_release == null && to_unscheduled == null)
			return true;
		else
			return false;
	}
	
	private boolean isRelease() {
		if(to_iteration == null && to_release != null && to_unscheduled == null)
			return true;
		else
			return false;
	}
	
	private boolean isIteration() {
		if(to_iteration != null && to_release == null && to_unscheduled == null)
			return true;
		else
			return false;
	}
	
	private boolean isUnscheduled() {
		if(to_iteration == null && to_release == null && to_unscheduled != null)
			return true;
		else
			return false;
	}
}
